#include "Vector3D.h"
#include <math.h>

Vector3D Vector3D::Add(const Vector3D& left, const Vector3D& right)
{
	Vector3D r;
	r.x = left.x + right.x;
	r.y = left.y + right.y;
	r.z = left.z + right.z;

	return r;
}

Vector3D Vector3D::Substract(const Vector3D& left, const Vector3D& right)
{
	Vector3D r;
	r.x = left.x - right.x;
	r.y = left.y - right.y;
	r.z = left.z - right.z;

	return r;
}

 Vector3D Vector3D::Scale(const Vector3D& vect, float scale)
{
	Vector3D scaleVect = {vect.x * scale, vect.y * scale, vect.z * scale};

	return scaleVect;
}

float Vector3D::Dot(const Vector3D& left, const Vector3D& right)
{
	float dot = (left.x * right.x) + (left.y * right.y) + (left.z * right.z);
	return dot;
}

Vector3D Vector3D::CrossLH(const Vector3D& left, const Vector3D& right)
{
	Vector3D cross;
	cross.x = (left.y * right.z) - (right.y * left.z);
	cross.y = -((left.x * right.z) - (right.x * left.z));
	cross.z = -((left.x * right.y) - (right.x * left.y));

	return cross;
}

Vector3D Vector3D::CrossRH(const Vector3D& left, const Vector3D& right)
{
	Vector3D cross;
	cross.x = (left.y * right.z) - (right.y * left.z);
	cross.y = -((left.x * right.z) - (right.x * left.z));
	cross.z = (left.x * right.y) - (right.x * left.y);

	return cross;
}

void Vector3D::MoveBy(const Vector3D& by, Vector3D& tomove)
{
	tomove.x += by.x;
	tomove.y += by.y;
	tomove.z += by.z;
}

float Vector3D::Length(const Vector3D& vect)
{
	float length = sqrt((pow(vect.x, 2)) + (pow(vect.y, 2)) + (pow(vect.z, 2)));
	return length;
}

float Vector3D::LengthSq(const Vector3D& vect)
{
	float lengthSq = (pow(vect.x, 2)) + (pow(vect.y, 2)) + (pow(vect.z, 2));
	return lengthSq;
}

bool Vector3D::Normalize(Vector3D& vect)
{
	float vectLength = Length(vect);
	
	if (vectLength != 0)
	{
		vect.x /= vectLength;
		vect.y /= vectLength;
		vect.z /= vectLength;
		return true;
	}

	return false;
}

void Vector3D::ScaleBy(Vector3D& vect, float scale)
{
	vect.x *= scale;
	vect.y *= scale;
	vect.z *= scale;
	return;
}

bool Vector3D::Equals(const Vector3D& left, const Vector3D& right)
{
	if ((fabs(left.x - right.x)) < EPSILON && (fabs(left.y - right.y)) < EPSILON && (fabs(left.z - right.z)) < EPSILON)
	{
		return true;
	}
	return false;
}
